package org.shangrila.bme.mb;

/**
 * 使用JSF Bean： new DbAccess()
 *  
 */

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.RowEditEvent;
import org.shangrila.app.sys.cfg.AppInitInfo;
import org.shangrila.bme.eo.BmeProdBom;
import org.shangrila.comm.dao.DbAccess;
import org.shangrila.comm.helper.IDHelper;
 
@Named
@SessionScoped
public class ProdBomMB implements Serializable {
	private static final long serialVersionUID = -5051492023664217108L;

	@Inject
	private DbAccess db;
	
	private List<BmeProdBom> vo = new ArrayList<BmeProdBom>(); //显示列表
    private BmeProdBom io;
    private BmeProdBom selected;
	private List<SelectItem> itemsMid;
	
	@Inject
	BmeCommEntityService bmeCommEntityService;
    
    @PostConstruct
    public void init() {
    	setItemsMid(AppInitInfo.getDropdownList("BmeMaterial","id","mdesc"));//查询结果作为下拉数据
    	newIo();  
    }

    public void newIo() {
    	io = new BmeProdBom();
    	io.setId(IDHelper.genStrIDs(16));
    }
    
	public String addAction() {
		db.beginTransaction();
		db.insert(io);
		db.commitTransaction();
		
		showMessage("录入", "信息录入成功");
		
		vo.add(io);
		newIo();
		return null;
	}
	
	public String queryAction() {		
		vo = db.findEntityAll("BmeProdBom");
		return null;
	}
    
    public void onRowEdit(RowEditEvent event) {
    	selected = vo.get(vo.indexOf((BmeProdBom) event.getObject()));
    	db.beginTransaction();
    	db.update(selected);
    	db.commitTransaction();
    	showMessage("编辑", "编辑保存成功。");
    }
     
    public void onRowCancel(RowEditEvent event) {
    	showMessage("编辑", "取消编辑成功。");
    }
    
	public void showMessage(String summary, String detail) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	
	public List<BmeProdBom> getVo() {
		return vo;
	}

	public void setVo(List<BmeProdBom> vo) {
		this.vo = vo;
	}

	public BmeProdBom getIo() {
		return io;
	}

	public void setIo(BmeProdBom io) {
		this.io = io;
	}

	public BmeProdBom getSelected() {
		return selected;
	}

	public void setSelected(BmeProdBom selected) {
		this.selected = selected;
	}

	public List<SelectItem> getItemsMid() {
		return itemsMid;
	}

	public void setItemsMid(List<SelectItem> itemsMid) {
		this.itemsMid = itemsMid;
	}
}